let mongoose = require("mongoose");
mongoose
  .connect("mongodb+srv://2264521353:1234567890@six0.hic1spu.mongodb.net/2407b")
  .then(() => {
    console.log("数据库连接成功");
  })
  .catch(() => {
    console.log("数据库连接失败");
  });
let User = mongoose.Schema({
  username: String,
  password: String,
  phone: String,
  image: String,
  sold: {
    type: Number,
    default: 0,
  },
  vip: {
    type: Number,
    default: 0,
  },
  like: {
    type: Array,
    default: [],
  },
  cart: {
    type: Array,
    default: [],
  },
  unreadMessages: {
    type: Map,
    of: Number,
    default: {}
  }
});

let Message = mongoose.Schema({
  from: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  to: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  content: {
    type: String,
    required: true
  },
  timestamp: {
    type: Date,
    default: Date.now
  },
  isRead: {
    type: Boolean,
    default: false
  }
});

let list = mongoose.Schema({
  title: String,
  desc: String,
  img: Array,
  price: Number,
  user_id: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  delivery: {
    type: Boolean,
    default: false,
  },
  collect: {
    type: Boolean,
    default: false,
  },
  type: String,
});
let CartList = mongoose.Schema({
  cart_id: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "list",
  },
  user_id: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  state: {
    type: Number,
    default: 0,
  },
});
let listModel = mongoose.model("list", list);
let userModel = mongoose.model("User", User);
let cartModel = mongoose.model("CartList", CartList);
let messageModel = mongoose.model("Message", Message);

module.exports = { listModel, userModel, cartModel, messageModel };
